<?php

namespace App\Service;

use App\Exceptions\ApiRequestException;
use Illuminate\Support\Facades\Cache;

class SmsService
{
    protected $seconds = 15*60;
     public function sendCode($phone,$type='register'){
         $code = makeCode(4);
         $key = $this->getKey($type, $phone);
         Cache::put($key, $code, $this->seconds);
//         dd(cache($key),$key);
    }

    public function validate($phone,$code, $type='register')
    {
        $key = $this->getKey($phone,$type);
        $cache_code = Cache::get($key);
        if ($code != $cache_code) {
            throw  new ApiRequestException('手机验证码不对');
        }
        Cache::forget($key);
    }

    public function getKey($type,$phone)
    {
        return $type.'_'.$phone;
    }
}
